package com.arpa.oms.controller;

import cn.hutool.core.util.StrUtil;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.domain.entity.Dict;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.ntocc.common.service.IDictService;
import com.arpa.oms.domain.vo.ChooseShopVo;
import com.arpa.wms.domain.consts.NumConst;
import com.arpa.wms.domain.dto.ShopDTO;
import com.arpa.wms.domain.entity.Shop;
import com.arpa.wms.domain.vo.ShopVO;
import com.arpa.wms.service.IShopService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * <p>
 * Oms店铺 Controller
 * </p>
 *
 * @author wangdong
 * @since 2021-07-21
 */
@Api(tags = "店铺管理")
@RestController
@RequestMapping(value = "/omsShop", produces = "application/json;charset=UTF-8")
@Log4j2
public class OmsShopController {

    private final IShopService iShopService;
    private final IDictService iDictService;

    public OmsShopController(IShopService iShopService, IDictService iDictService) {
        this.iShopService = iShopService;
        this.iDictService = iDictService;
    }

    /**
     * 获取 店铺 列表
     *
     * @param shopDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 店铺 列表", notes = "获取 店铺 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = ShopVO.class))
    @NeedLogin
    @PostMapping("/list")
    public Result list(ShopDTO shopDTO, HttpServletRequest request) {
        try {
            shopDTO.setShipmentCode(UserUtil.getLoginWarehouseCodeOrShipmentCode());
            ShopVO shopVOSum = iShopService.queryListSum(shopDTO);
            List<ShopVO> shopVOList = iShopService.queryList(shopDTO);
            for (ShopVO c : shopVOList){
                if (StrUtil.isNotBlank(c.getSourcePlatformCode())){
                    Dict dict = iDictService.getOne(new QueryWrapper<Dict>()
                            .lambda().eq(Dict::getCode, c.getSourcePlatformCode()).eq(Dict::getType, "salePlatform"));
                    c.setSourcePlatformName(dict.getName());
                }
            }
            Page returnPage = new Page(shopDTO.getPageNum(), shopDTO.getPageSize());
            returnPage.setRecords(shopVOList);
            returnPage.setTotal(shopVOSum.getTotal());
            returnPage.setSumObject(shopVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    @ApiOperation(value = "OMS销售单获取入驻商下审核通过店铺列表", notes = "OMS销售单获取入驻商下审核通过店铺列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = ChooseShopVo.class))
    @NeedLogin
    @GetMapping("/chooseData")
    public Result chooseData() {
        List<Shop> list = iShopService.list(new QueryWrapper<Shop>().lambda()
                .select(Shop::getCode,Shop::getName)
                .eq(Shop::getShipmentCode,  UserUtil.getLoginWarehouseCodeOrShipmentCode())
                .eq(Shop::getStatus, NumConst.NUM_ONE)
                .orderByAsc(Shop::getName));
        return Result.ok().data(list);
    }

}
